package com.jabo.wechatmomentsdemo.ui.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jabo.wechatmomentsdemo.R;
import com.jabo.wechatmomentsdemo.model.bean.TweetBean;
import com.jabo.wechatmomentsdemo.model.bean.UserInfo;
import com.jabo.wechatmomentsdemo.utils.GlideUtil;
import com.jabo.wechatmomentsdemo.widget.CommentsLinearLayout;
import com.jabo.wechatmomentsdemo.widget.PengYouQuanFlagView;

import java.util.ArrayList;
import java.util.List;

/**
 * @anthor JZheng3
 * @Time: 2020/9/4
 */
public class MomentsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int HEAD_SIZE = 1;
    private final static int HEADER_TYPE = -9;
    private final static int CONTENT_TYPE = -10;
    private static final int HEADER_POSITION = 0;

    public MomentsListAdapter() {
        mTweetBeans = new ArrayList<>();
    }

    private List<TweetBean> mTweetBeans;
    private UserInfo mUserInfo;

    public void addTweetListData(@NonNull List<TweetBean> beans) {
        int startIndex = mTweetBeans.size();
        mTweetBeans.addAll(beans);
        notifyItemRangeChanged(startIndex, beans.size());
    }

    public void setTweetListData(@NonNull List<TweetBean> beans) {
        mTweetBeans.clear();
        mTweetBeans.addAll(beans);
        notifyDataSetChanged();
    }

    public void setUserInfo(UserInfo userInfo) {
        mUserInfo = userInfo;
        notifyItemChanged(HEADER_POSITION);
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeaderType(position)) {
            return HEADER_TYPE;
        } else {
            return CONTENT_TYPE;
        }
    }

    public List<TweetBean> getTweetBeans() {
        return mTweetBeans;
    }

    private boolean isHeaderType(int position) {
        return position == 0;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == HEADER_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_moments_header, parent, false);
            return new MomentsHeaderViewHolder(view);
        } else if (viewType == CONTENT_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_moments_content, parent, false);
            return new MomentsContentViewHolder(view);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (isHeaderType(position)) {
            if (mUserInfo != null) {
                MomentsHeaderViewHolder momentsViewHolder = (MomentsHeaderViewHolder) holder;
                Context context = momentsViewHolder.mNameTextView.getContext();
                momentsViewHolder.mNameTextView.setText(mUserInfo.getUsername());
                GlideUtil.loadUrlHandleErrorImage(context, mUserInfo.getAvatar(), momentsViewHolder.mAvatarImageView);
                GlideUtil.loadUrlHandleErrorImage(context, mUserInfo.getProfileimage(), momentsViewHolder.mBgImageView);
            }

        } else {
            TweetBean tweetBean = mTweetBeans.get(position - HEAD_SIZE);
            MomentsContentViewHolder momentsViewHolder = (MomentsContentViewHolder) holder;
            Context context = momentsViewHolder.mCommentsLinearLayout.getContext();
            TweetBean.SenderBean sender = tweetBean.getSender();
            List<Spannable> comments = tweetBean.getCommentsString();
            momentsViewHolder.mSenderNameTextView.setText(sender.getNick());
            momentsViewHolder.mSenderContentTextView.setText(tweetBean.getContent());
            momentsViewHolder.mSenderContentTextView.setVisibility(TextUtils.isEmpty(tweetBean.getContent()) ? View.GONE : View.VISIBLE);
            momentsViewHolder.mNinePictureLayout.setImgList(tweetBean.getImageStrings());
            momentsViewHolder.mCommentsLinearLayout.setDatas(comments);
            GlideUtil.loadUrlHandleErrorImage(context, sender.getAvatar(), momentsViewHolder.mSenderAvatarImageView);
        }
    }

    @Override
    public int getItemCount() {
        return mTweetBeans == null ? HEAD_SIZE : mTweetBeans.size() + HEAD_SIZE;
    }

    static class MomentsHeaderViewHolder extends RecyclerView.ViewHolder {
        private ImageView mBgImageView;
        private ImageView mAvatarImageView;
        private TextView mNameTextView;

        public MomentsHeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            mBgImageView = itemView.findViewById(R.id.iv_header_bg);
            mAvatarImageView = itemView.findViewById(R.id.iv_avatar);
            mNameTextView = itemView.findViewById(R.id.tv_name);
        }
    }

    static class MomentsContentViewHolder extends RecyclerView.ViewHolder {

        private ImageView mSenderAvatarImageView;
        private TextView mSenderNameTextView;
        private TextView mSenderContentTextView;
        private TextView mSenderTimeTextView;
        private PengYouQuanFlagView mNinePictureLayout;
        private CommentsLinearLayout mCommentsLinearLayout;

        public MomentsContentViewHolder(@NonNull View itemView) {
            super(itemView);
            mSenderAvatarImageView = itemView.findViewById(R.id.iv_sender_avatar);
            mSenderNameTextView = itemView.findViewById(R.id.tv_sender_name);
            mSenderTimeTextView = itemView.findViewById(R.id.tv_time);
            mSenderContentTextView = itemView.findViewById(R.id.tv_content);
            mNinePictureLayout = itemView.findViewById(R.id.layout_nine_view);
            mCommentsLinearLayout = itemView.findViewById(R.id.ll_comments);
        }
    }
}
